package com.soilflow.web.controller.soilflow;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.soilflow.common.annotation.Log;
import com.soilflow.common.core.controller.BaseController;
import com.soilflow.common.core.domain.AjaxResult;
import com.soilflow.common.core.domain.entity.SysDept;
import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.common.core.domain.entity.SysUserReception;
import com.soilflow.common.core.page.TableDataInfo;
import com.soilflow.common.domain.LandDeptItem;
import com.soilflow.common.domain.LandDeptList;
import com.soilflow.common.domain.LandInfo;
import com.soilflow.common.domain.dto.LandNewsDto;
import com.soilflow.common.domain.vo.LandNewsVo;
import com.soilflow.common.enums.BusinessType;
import com.soilflow.common.utils.poi.ExcelUtil;
import com.soilflow.framework.web.domain.server.Sys;
import com.soilflow.info.service.LandDeptItemService;
import com.soilflow.info.service.LandDeptListService;
import com.soilflow.info.service.LandNewsService;
import com.soilflow.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 村民土地占用信息
 */
@RestController
@RequestMapping("/land/dept")
public class LandDeptListController extends BaseController {

    @Autowired
    private LandNewsService landNewsService;
    @Autowired
    private LandDeptListService landDeptListService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private LandDeptItemService landDeptItemService;




    /**
     * 新闻列表
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo landDeptList(LandDeptList dto)
    {
        startPage();
        List<LandDeptList> list = landDeptListService.queryList(dto);
        return getDataTable(list);
    }


    /**
     * 添加户信息
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addDeptList(@RequestBody LandDeptList dto){

        QueryWrapper<SysDept> query = new QueryWrapper<>();
        query.lambda().eq(SysDept::getDeptId, dto.getDeptId());
        SysDept deptId = sysDeptService.getOne(query);
        System.out.println(deptId);
        if(deptId==null){
            return AjaxResult.error("无效的行政村");
        }

        QueryWrapper<SysDept> query2 = new QueryWrapper<>();
        query2.lambda().eq(SysDept::getDeptId, deptId.getParentId());
        SysDept deptTown = sysDeptService.getOne(query2);
        if(deptTown==null){
            return AjaxResult.error("无效的行政村数据");
        }

        dto.setTown(deptTown.getDeptName());
        dto.setVillage(deptId.getDeptName());
        Boolean result = landDeptListService.save(dto);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error("请勿重复添加");
    }


    /**
     * 添加户成员信息
     * @param dto
     * @return
     */
    @PostMapping("/addItem")
    public AjaxResult addDeptItem(@RequestBody LandDeptItem dto){
        Boolean result = landDeptItemService.save(dto);
        if (result){
            return AjaxResult.success();
        }
        return AjaxResult.error("请勿重复添加");
    }

    @PutMapping("/updateItem")
    public AjaxResult updateDeptItem(@RequestBody LandDeptItem dto){
        landDeptItemService.updateById(dto);
        return AjaxResult.success("操作成功");
    }

    @DeleteMapping("/delItem/{id}")
    public AjaxResult delLandDeptItem(@PathVariable Long id){
        landDeptItemService.removeById(id);
        return AjaxResult.success("操作成功");
    }

    @GetMapping("/listItem")
    public TableDataInfo landDeptItemList(LandDeptItem dto)
    {
        startPage();
        List<LandDeptItem> list = landDeptItemService.queryList(dto);
        return getDataTable(list);
    }




    /**
     * 删除新闻
     * @param ids
     * @return
     */
    @DeleteMapping("/dels/{ids}")
    public AjaxResult delLandNews(@PathVariable List<Long> ids){
        landNewsService.removeByIds(ids);
        return AjaxResult.success();
    }
    /**
     * 删除新闻
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public AjaxResult delLandDeptList(@PathVariable Long id){
        landDeptListService.removeById(id);
        return AjaxResult.success();
    }
    /**
     * 更新新闻
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public AjaxResult updateLandNews(@RequestBody LandDeptList dto){
        landDeptListService.updateById(dto);
        return AjaxResult.success();
    }

    /**
     * 回显详情
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getLandNewsById(@PathVariable Long id){
        LandNewsVo vo = landNewsService.getLandNewsById(id);
        return AjaxResult.success(vo);
    }



    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<LandDeptList> util = new ExcelUtil<LandDeptList>(LandDeptList.class);
        List<LandDeptList> userList = util.importExcel(file.getInputStream());
        for (LandDeptList landDeptList : userList) {
            System.err.println(landDeptList);
            // 查询村数据
            QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysDept::getDeptName, landDeptList.getVillage()).last("limit 1");
            SysDept one = sysDeptService.getOne(queryWrapper);
            if(one != null){
                // 查询乡镇数据
                QueryWrapper<SysDept> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.lambda().eq(SysDept::getDeptId, one.getParentId()).last("limit 1");
                SysDept one2 = sysDeptService.getOne(queryWrapper2);

                landDeptList.setTown(one2.getDeptName());
                landDeptList.setDeptId(one.getDeptId());
                landDeptListService.save(landDeptList);
            }
        }
        return AjaxResult.success("导入成功");
    }



    @PostMapping("/importMemberData")
    public AjaxResult importMemberData(MultipartFile file) throws Exception
    {
        ExcelUtil<LandDeptItem> util = new ExcelUtil<LandDeptItem>(LandDeptItem.class);
        List<LandDeptItem> userList = util.importExcel(file.getInputStream());
        System.err.println(userList);
        for (LandDeptItem landDeptItem: userList) {
            // 查询村数据
            QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysDept::getDeptName, landDeptItem.getVillage()).last("limit 1");
            SysDept one = sysDeptService.getOne(queryWrapper);
            if(one != null){
                // 查询乡镇数据
                QueryWrapper<SysDept> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.lambda().eq(SysDept::getDeptId, one.getParentId()).last("limit 1");
                SysDept one2 = sysDeptService.getOne(queryWrapper2);

                landDeptItem.setTown(one2.getDeptName());
                landDeptItem.setDeptId(one.getDeptId());
                landDeptItemService.save(landDeptItem);
            }
        }
        return AjaxResult.success("导入成功");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        System.out.println("DDDDDDDDDDDDDDDDD");
        ExcelUtil<LandDeptList> util = new ExcelUtil<LandDeptList>(LandDeptList.class);
        util.importTemplateExcel( "农民耕地明细");
    }



}
